﻿#region Using Statements
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

using TInput = starLiGHT.Content.SpriteListContent;
using TOutput = starLiGHT.SpriteSheetContent;
#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Content.

    starLiGHT.Content is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Content is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Content.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Content are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1514                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-10-12 10:12:09 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Content
{
    [ContentProcessor(DisplayName = "SpriteSheet Processor - starLiGHT.Engine")]
    public class SpriteSheetProcessor : ContentProcessor<TInput, TOutput>
    {
        public override TOutput Process(TInput input, ContentProcessorContext context)
        {
            SpriteSheetContent spriteSheet = new SpriteSheetContent();
            List<BitmapContent> sourceSprites = new List<BitmapContent>();

            foreach (string file in input.Files)
            {
                try
                {
                    TextureContent tex = context.BuildAndLoadAsset<TextureContent, TextureContent>(new ExternalReference<TextureContent>(file), "TextureProcessor");
                    foreach (MipmapChain mc in tex.Faces)
                    {
                        foreach (BitmapContent bc in mc)
                        {
                            sourceSprites.Add(bc);
                            string spriteName = Path.GetFileNameWithoutExtension(file);
                            spriteSheet.SpriteNames.Add(spriteName, sourceSprites.Count);
                        }
                    }

                }
                catch { }
            }

            // Pack all the sprites into a single large texture.
            BitmapContent packedSprites = SpritePacker.PackSprites(sourceSprites,
                                                                   spriteSheet.SpriteRectangles,
                                                                   context);

            spriteSheet.Texture.Mipmaps.Add(packedSprites);

            return spriteSheet;
        }

    }
}